<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/6/29
 * Time: 21:30
 */

namespace app\admin\controller;
use app\admin\controller\Base;
use app\admin\model\Bp;
use app\admin\model\Register;
use think\Db;
use think\Config;
use app\index\controller\Index;

class Qtgl extends Base
{
    public function index(){
        $list = Register::alias('a')
                    ->join('register_cs b','a.id=b.reg_id','LEFT')
                    ->join('register_cb c','a.id=c.reg_id','LEFT')
                    ->join('register_gbt d','a.id=d.reg_id','LEFT')
                    ->join('register_js e','a.id=e.reg_id','LEFT')
                    ->field('a.*,b.cs_datetime,b.cs_fjid,b.cs_price,
                                c.cbrq,c.ydl,c.ydlc,c.ydlfy,c.cbfs,c.cbfy,
                                d.gbt,d.gbtfy,e.ytysfs_id,e.ytysfs,e.jsfy')
                    ->where('djbz',0)       // 只显示前台订单
                    ->where('a.sy|a.xm|a.sfzh|a.jtzz|a.lxr|a.lxdh|a.zbr|a.lxrsfz|a.kkszd','like','%'.$this->request->param('keywords').'%')
                    ->select();
        $totalCounts = Register::where('djbz',0)->count();
        $this->assign('list',$list);
        $this->assign('totalcounts',$totalCounts);
        return $this->fetch('rcyw/qtgl/index');
    }

    // 新增订单
    public function adddj(){
        // 登记日期
        $this->assign('djrq',date('Y-m-d H:i:s'));
        //  值班人
        $this->assign('zbr',\app\admin\model\Zbr::all());
        //  司仪
        $this->assign('sy',\app\admin\model\Sy::all());
        //  死亡原因
        $this->assign('swyy',\app\admin\model\Swyy::all());
        //  证件
        $this->assign('zj',\app\admin\model\Zj::all());
        //  死亡日期
        $this->assign('swrq',date('Y-m-d'));
        //  证件
        $this->assign('jmlx',\app\admin\model\Jmlx::all());
        //  与逝者关系
        $this->assign('yszgx',\app\admin\model\Yszgx::all());
        //  遗体运送方式
        $this->assign('ytysfs',\app\admin\model\Ytysfs::all());
        //  房间号
        $this->assign('fjxx',\app\admin\model\Fjxx::all(['status'=>0]));
        //  预定炉
        $this->assign('hhl',\app\admin\model\Hhl::all());
        //  出殡方式
        $this->assign('cbfs',\app\admin\model\Cbfs::all());
        //  告别厅
        $this->assign('gbt',\app\admin\model\Gbt::all());
        //  减免类型
        $this->assign('jmlx',\app\admin\model\Jmlx::all());

        return $this->fetch('rcyw/qtgl/adddj');
    }

    public function adddjdo(){
        $data = array(
            'djrq' => $this->request->param('djrq'),
            'xm' => $this->request->param('xm'),                // 姓名
            'xb' => $this->request->param('xb'),                // 性别
            'nl' => $this->request->param('nl'),                // 年龄
            'sfzh' => $this->request->param('sfzh'),            // 身份证号
            'jtzz' => $this->request->param('jtzz'),            // 家庭住址
            'swyy' => $this->request->param('swyy'),            // 死亡原因
            'swrq' => $this->request->param('swrq'),            // 死亡日期
            'lxr' => $this->request->param('lxr'),              // 联系人
            'yszgx' => $this->request->param('yszgx'),          // 与逝者关系
            'lxdh' => $this->request->param('lxdh'),            // 联系电话
            'zj' => $this->request->param('zj'),                // 证件
            'bz' => $this->request->param('bz'),                // 备注
            'qtfj' => $this->request->param('qtfj'),            // 前台附加
            'czybh' => $this->request->session('efing.uname'),  // 操作用户
            'czrq' => time(),                                           // 操作日期时间
            'is_cs' => $this->request->param('is_cs'),          // 是否存尸
            'is_dlcb' => $this->request->param('is_dlcb'),      // 是否定炉出殡
            'zbr' => $this->request->param('zbr'),              // 值班人
            'sy' => $this->request->param('sy'),                // 司仪
            'djbz' => 0,                                                // 登记部门标志
            'mz' => $this->request->param('mz'),                // 民族
            'is_bdhk' => $this->request->param('is_bdhk'),      // 是否本地户口
            'zzjgdm' => Config::get('app_zzjgdm'),              // 组织机构代码
            'lxrsfz' => $this->request->param('lxrsfz'),        // 联系人身份证
            'jmlx' => $this->request->param('jmlx'),            // 减免类型
            'sfzlx' => $this->request->param('sfzlx'),          // 身份证类型
            'gjhdq' => $this->request->param('gjhdq'),          // 国家或地区
            'dwmc' => Config::get('app_dwmc'),                  // 火化单位名称
            'kkszd' => $this->request->param('kkszd'),          // 户口所在地
        );
        if(count($data)>=22){
            $check = Register::get(['sfzh'=>$data['sfzh']]);
            if($check){
                $res = ['statusCode'=>300,'message'=>'登记信息重复'];
            }else{
                if($insert = Register::create($data)){
                    $indexObj = new Index();
                    if($ddh = $indexObj->formatDddh($insert->id,0)){
                        Register::update(['ddh'=>$ddh,'id'=>$insert->id]);
                    }
                    $user = \app\admin\model\User::get(['uname'=>$this->request->session('efing.uname')]);
                    // 处理接尸方式和费用
                    $ytysfs = \app\admin\model\Ytysfs::get(['title'=>$this->request->param('ytysfs')]);
                    if($ytysfs){
                        $ytysfs_data = array(
                            'ytysfs_id' => $ytysfs->id,
                            'ytysfs' => $ytysfs->title,
                            'jsfy' => $this->request->param('jsfy'),
                            'opt_user' => $user->uname,
                            'opt_datetime' => time(),
                            'opt_name' => $user->name,
                            'reg_id' => $insert->id,
                            'create_time' => time()
                        );
                        if(count($ytysfs_data)>=8){
                            $check_ytysfs = Db::name('register_js')->where('reg_id',$insert->id)->find();
                            if($check_ytysfs==false){
                                Db::name('register_js')->insert($ytysfs_data);
                            }
                        }
                    }
                    // 处理存尸信息
                    if($data['is_cs']==1){
                        $cs_data = array(
                            'reg_id' => $insert->id,
                            'cs_datetime' => strtotime($this->request->param('cs_datetime')),
                            'cs_fjid' => $this->request->param('cs_fjid'),
                            'cs_price' => $this->request->param('cs_price'),
                            'opt_user' => $user->uname,
                            'opt_datetime' => time(),
                            'opt_name' => $user->name,
                            'create_time' => time()
                        );
                        if(false == Db::name('register_cs')->where('reg_id',$insert->id)->find() and count($cs_data)>=8){
                            Db::name('register_cs')->insert($cs_data);
                        }
                    }
                    // 处理出殡
                    if ($data['is_dlcb']==1){
                        $hhl = \app\admin\model\Hhl::get($this->request->param('ydl'));
                        $hhllc = \app\admin\model\Hhllc::get($this->request->param('ydlc'));
                        $cbfs = \app\admin\model\Cbfs::get(['title'=>$this->request->param('cbfs')]);
                        $cb_data = array(
                            'reg_id' => $insert->id,
                            'create_time' => time(),
                            'opt_user' => $user->uname,
                            'opt_datetime' => time(),
                            'opt_name' => $user->name,
                            'cbrq' => strtotime($this->request->param('cbrq')),
                            'ydl' => $hhl->title,
                            'ydlfy' => $hhl->price,
                            'ydlc' => $hhllc->hhllc,
                            'ydlcid' => $hhllc->id,
                            'cbfs' => $this->request->param('cbfs'),
                            'cbfy' => $this->request->param('cbfy'),
                            'cbfsid' => $cbfs->id,
                        );
                        if(count($cb_data)>=13 and false==Db::name('register_cb')->where('reg_id',$insert->id)->find()){
                            Db::name('register_cb')->insert($cb_data);
                        }
                    }
                    // 处理告别厅
                    if($gbt = $this->request->param('gbt')){
                        $gbtfy = $this->request->param('gbtfy');
                        $gbt_data = array(
                            'reg_id' => $insert->id,
                            'create_time' => time(),
                            'opt_user' => $user->uname,
                            'opt_datetime' => time(),
                            'opt_name' => $user->name,
                            'gbt' => $gbt,
                            'gbtfy' => $gbtfy?$gbtfy:0
                        );
                        if(count($gbt_data)>=6 and false==Db::name('register_gbt')->where('reg_id',$insert->id)->find()){
                            Db::name('register_gbt')->insert($gbt_data);
                        }
                    }
                    //echo '200';
                    if($this->request->param('btn_type')=='1'){
                        $res = ['statusCode'=>200,'message'=>'订单保存成功并打印中...'];
                    }else{
                        $res = ['statusCode'=>200,'message'=>'订单保存成功'];
                    }
                }else{
                    //echo '300';
                    $res = ['statusCode'=>300,'message'=>'操作失败'];
                }
            }

        }else{
            //echo '300';
            $res = ['statusCode'=>300,'message'=>'请正确输入登记信息'];
        }
        echo json_encode($res);
    }

    // 修改订单
    public function editdj(){
        // 登记日期
        $this->assign('djrq',date('Y-m-d H:i:s'));
        //  值班人
        $this->assign('zbr',\app\admin\model\Zbr::all());
        //  司仪
        $this->assign('sy',\app\admin\model\Sy::all());
        //  死亡原因
        $this->assign('swyy',\app\admin\model\Swyy::all());
        //  证件
        $this->assign('zj',\app\admin\model\Zj::all());
        //  死亡日期
        $this->assign('swrq',date('Y-m-d'));
        //  证件
        $this->assign('jmlx',\app\admin\model\Jmlx::all());
        //  与逝者关系
        $this->assign('yszgx',\app\admin\model\Yszgx::all());
        //  遗体运送方式
        $this->assign('ytysfs',\app\admin\model\Ytysfs::all());
        //  房间号
        $this->assign('fjxx',\app\admin\model\Fjxx::all(['status'=>0]));
        //  预定炉
        $this->assign('hhl',\app\admin\model\Hhl::all());
        //  出殡方式
        $this->assign('cbfs',\app\admin\model\Cbfs::all());
        //  告别厅
        $this->assign('gbt',\app\admin\model\Gbt::all());
        //  减免类型
        $this->assign('jmlx',\app\admin\model\Jmlx::all());

        // 获取订单id
        $id = $this->request->param('id');
        $this->assign('info',Register::get($id));
        $this->assign('cbinfo',Db::name('register_cb')->where('reg_id',$id)->find());
        $this->assign('csinfo',Db::name('register_cs')->where('reg_id',$id)->find());
        $this->assign('gbtinfo',Db::name('register_gbt')->where('reg_id',$id)->find());
        $this->assign('jsinfo',Db::name('register_js')->where('reg_id',$id)->find());
        
        return $this->fetch('rcyw/qtgl/editdj');
    }

    public function editdjdo(){
        $data = array(
            'id' => $this->request->param('id'),
            'djrq' => $this->request->param('djrq'),
            'xm' => $this->request->param('xm'),                // 姓名
            'xb' => $this->request->param('xb'),                // 性别
            'nl' => $this->request->param('nl'),                // 年龄
            'sfzh' => $this->request->param('sfzh'),            // 身份证号
            'jtzz' => $this->request->param('jtzz'),            // 家庭住址
            'swyy' => $this->request->param('swyy'),            // 死亡原因
            'swrq' => $this->request->param('swrq'),            // 死亡日期
            'lxr' => $this->request->param('lxr'),              // 联系人
            'yszgx' => $this->request->param('yszgx'),          // 与逝者关系
            'lxdh' => $this->request->param('lxdh'),            // 联系电话
            'zj' => $this->request->param('zj'),                // 证件
            'bz' => $this->request->param('bz'),                // 备注
            'qtfj' => $this->request->param('qtfj'),            // 前台附加
            'czybh' => $this->request->session('efing.uname'),  // 操作用户
            //'czrq' => time(),                                           // 操作日期时间
            'is_cs' => $this->request->param('is_cs'),          // 是否存尸
            'is_dlcb' => $this->request->param('is_dlcb'),      // 是否定炉出殡
            'zbr' => $this->request->param('zbr'),              // 值班人
            'sy' => $this->request->param('sy'),                // 司仪
            'djbz' => 0,                                                // 登记部门标志
            'mz' => $this->request->param('mz'),                // 民族
            'is_bdhk' => $this->request->param('is_bdhk'),      // 是否本地户口
            'zzjgdm' => Config::get('app_zzjgdm'),              // 组织机构代码
            'lxrsfz' => $this->request->param('lxrsfz'),        // 联系人身份证
            'jmlx' => $this->request->param('jmlx'),            // 减免类型
            'sfzlx' => $this->request->param('sfzlx'),          // 身份证类型
            'gjhdq' => $this->request->param('gjhdq'),          // 国家或地区
            'dwmc' => Config::get('app_dwmc'),                  // 火化单位名称
            'kkszd' => $this->request->param('kkszd'),          // 户口所在地
        );
        if(count($data)>=22){
            $check = Register::where(['sfzh'=>$data['sfzh']])->where('id','<>',$data['id'])->find();
            if($check){
                $res = ['statusCode'=>300,'message'=>'登记信息重复'];
            }else{
                if($update = Register::update($data)){
                    $user = \app\admin\model\User::get(['uname'=>$this->request->session('efing.uname')]);
                    // 处理接尸方式和费用
                    $ytysfs = \app\admin\model\Ytysfs::get(['title'=>$this->request->param('ytysfs')]);
                    if($ytysfs){
                        $ytysfs_data = array(
                            'ytysfs_id' => $ytysfs->id,
                            'ytysfs' => $ytysfs->title,
                            'jsfy' => $this->request->param('jsfy'),
                            'opt_user' => $user->uname,
                            'opt_datetime' => time(),
                            'opt_name' => $user->name,
                            'reg_id' => $update->id,
                            'create_time' => time()
                        );
                        if(count($ytysfs_data)>=8){
                            if(Db::name('register_js')->where('reg_id',$update->id)->find()){
                                Db::name('register_js')->where('reg_id',$update->id)->update($ytysfs_data);
                            }else{
                                Db::name('register_js')->insert($ytysfs_data);
                            }
                        }
                    }
                    // 处理存尸信息
                    if($data['is_cs']==1){
                        $cs_data = array(
                            'reg_id' => $update->id,
                            'cs_datetime' => strtotime($this->request->param('cs_datetime')),
                            //'cs_price' => $this->request->param('cs_price'),
                            'opt_user' => $user->uname,
                            'opt_datetime' => time(),
                            'opt_name' => $user->name,
                            'create_time' => time()
                        );
                        if(count($cs_data)>=6){
                            if($cs_fjid = $this->request->param('cs_fjid')){
                                $cs_data['cs_fjid'] = $cs_fjid;
                                $cs_data['cs_price'] = $this->request->param('cs_price');
                            }
                            if(Db::name('register_cs')->where('reg_id',$update->id)->find()){
                                Db::name('register_cs')->where('reg_id',$update->id)->update($cs_data);
                            }else{
                                Db::name('register_cs')->insert($cs_data);
                            }
                        }
                    }else{
                        Db::name('register_cs')->where('reg_id',$update->id)->delete();
                    }
                    // 处理出殡
                    if ($data['is_dlcb']==1){
                        $hhl = \app\admin\model\Hhl::get($this->request->param('ydl'));
                        $hhllc = \app\admin\model\Hhllc::get($this->request->param('ydlc'));
                        $cbfs = \app\admin\model\Cbfs::get(['title'=>$this->request->param('cbfs')]);
                        $cb_data = array(
                            'reg_id' => $update->id,
                            'create_time' => time(),
                            'opt_user' => $user->uname,
                            'opt_datetime' => time(),
                            'opt_name' => $user->name,
                            'cbrq' => strtotime($this->request->param('cbrq')),
                        );
                        if(count($cb_data)>=6){
                            if($hhl){
                                $cb_data['ydl'] = $hhl->title;
                                $cb_data['ydlfy'] = $hhl->price;
                            }
                            if($hhllc){
                                $cb_data['ydlc'] = $hhllc->id;
                                $cb_data['ydlcid'] = $hhllc->id;
                            }
                            if($cbfs){
                                $cb_data['cbfs'] = $this->request->param('cbfs');
                                $cb_data['cbfy'] = $this->request->param('cbfy');
                                $cb_data['cbfsid'] = $cbfs->id;
                            }
                            if(false==Db::name('register_cb')->where('reg_id',$update->id)->find()){
                                Db::name('register_cb')->insert($cb_data);
                            }else{
                                Db::name('register_cb')->where('reg_id',$update->id)->update($cb_data);
                            }
                        }
                    }else{
                        Db::name('register_cb')->where('reg_id',$update->id)->delete();
                    }
                    // 处理告别厅
                    if($gbt = $this->request->param('gbt')){
                        $gbtfy = $this->request->param('gbtfy');
                        $gbt_data = array(
                            'reg_id' => $update->id,
                            'create_time' => time(),
                            'opt_user' => $user->uname,
                            'opt_datetime' => time(),
                            'opt_name' => $user->name,
                            'gbt' => $gbt,
                            'gbtfy' => $gbtfy?$gbtfy:0
                        );
                        if(count($gbt_data)>=6){
                            if(false==Db::name('register_gbt')->where('reg_id',$update->id)->find()){
                                Db::name('register_gbt')->insert($gbt_data);
                            }else{
                                Db::name('register_gbt')->where('reg_id',$update->id)->update($gbt_data);
                            }
                        }
                    }
                    $tjbtn = $this->request->param('btn_type');
                    if($tjbtn==1){
                        Register::where('id',$data['id'])->update(['qttjcjbz'=>1]);
                    }
                    if($tjbtn==2){
                        Register::where('id',$data['id'])->update(['qttjcwbz'=>1]);
                    }

                    $res = ['statusCode'=>200,'message'=>'订单保存成功'];
                }else{
                    $res = ['statusCode'=>300,'message'=>'操作失败'];
                }
            }
        }else{
            $res = ['statusCode'=>300,'message'=>'请正确输入登记信息'];
        }
        echo json_encode($res);

    }

    // 提交车间
    public function tjcj(){
        $ids = explode(',',$this->request->param('ids'));
        if(count($ids)>0){
            foreach($ids as $key=>$value){
                Register::where('id',$value)->update(['qttjcjbz'=>1,'tjcj_datetime'=>time()]);
            }
            $res = ['statusCode'=>200,'message'=>'操作完成'];
        }else{
            $res = ['statusCode'=>300,'message'=>'操作失败'];
        }
        echo json_encode($res);
    }

    // 提交财务
    public function tjcw(){
        $ids = explode(',',$this->request->param('ids'));
        if(count($ids)>0){
            foreach($ids as $key=>$value){
                Register::where('id',$value)->update(['qttjcwbz'=>1,'tjcw_datetime'=>time()]);
            }
            $res = ['statusCode'=>200,'message'=>'操作完成'];
        }else{
            $res = ['statusCode'=>300,'message'=>'操作失败'];
        }
        echo json_encode($res);
    }

    // 打印订单
    public function printdj(){
        // 获取订单id
        $id = $this->request->param('id');
        $this->assign('info',Register::get($id));
        $this->assign('cbinfo',Db::name('register_cb')->where('reg_id',$id)->find());
        $this->assign('csinfo',Db::name('register_cs')->where('reg_id',$id)->find());
        $this->assign('gbtinfo',Db::name('register_gbt')->where('reg_id',$id)->find());
        $this->assign('jsinfo',Db::name('register_js')->where('reg_id',$id)->find());
        $this->assign('qzrq',date('Y年m月d日'));
        $this->assign('show',$this->request->param('show'));
        Register::where('id',$id)->update(['is_printdj'=>1]);
        $res = ['statusCode'=>200,'message'=>'操作完成','data'=>$this->fetch('rcyw/qtgl/printdj')];
        echo json_encode($res);
    }

    // 删除订单
    public function deletedj(){
        $ids = explode(',',$this->request->param('ids'));
        if(count($ids)>0){
            foreach($ids as $key=>$value){
                $reg = Register::get($value);
                if($reg->qttjbz==0 and $reg->qttjcjbz!=1 and $reg->qttjcwbz!=1){
                   Register::destroy($value);
                   $cb = Db::name('register_cb')->where('reg_id',$value)->find();
                   Db::name('register_cb')->where('reg_id',$value)->delete();
                   Db::name('register_cs')->where('reg_id',$value)->delete();
                   Db::name('register_gbt')->where('reg_id',$value)->delete();
                   Db::name('register_js')->where('reg_id',$value)->delete();
                   \app\admin\model\Fjxx::where('id',$cb['ydlcid'])->update(['status'=>0]);
                }
            }
            $res = ['statusCode'=>200,'message'=>'操作完成'];
        }else{
            $res = ['statusCode'=>300,'message'=>'操作失败'];
        }
        echo json_encode($res);
    }

    // 添加殡品
    public function addbp(){
        // 获取订单id
        $id = $this->request->param('id');
        $this->assign('info',Register::get($id));
        $this->assign('cbinfo',Db::name('register_cb')->where('reg_id',$id)->find());
        $this->assign('csinfo',Db::name('register_cs')->where('reg_id',$id)->find());
        $this->assign('gbtinfo',Db::name('register_gbt')->where('reg_id',$id)->find());
        $this->assign('jsinfo',Db::name('register_js')->where('reg_id',$id)->find());
        $this->assign('qzrq',date('Y年m月d日'));
        $this->assign('optuser',$this->request->session('efing.uname'));
        // 成套殡品
        $this->assign('ctbplist',\app\admin\model\Ctbp::all());
        // 项目信息
        $this->assign('xmxxlist',\app\admin\model\Xmxx::order('xmbm','asc')->select());
        // 单位信息
        $this->assign('jldwlist',\app\admin\model\Jldw::all());
        return $this->fetch('rcyw/qtgl/addbp');
    }

    public function addbpdo(){
        $datas = $this->request->param('datas');
        $arr =  json_decode($datas,true);
        $id = $this->request->param('regid');
        $btn_type = $this->request->param('btn_type');
        $insert = array();
        if(count($arr)>0 and $id){
            $user = \app\admin\model\User::get(['uname'=>$this->request->session('efing.uname')]);
            foreach($arr as $k=>$v){
                $insert[$k] = array(
                    'id'=>$v[0],'xmbh' => $v[1],'xmmc' => $v[2],'dw' => $v[3],'sj' => $v[4],
                    'sl' => $v[5],'je' => $v[6],'xmbm' => $v[7],'xmid' => $v[8],'regid'=>$id,
                    'opt_user' => $user->uname,'opt_datetime' => time(),'opt_name' => $user->name
                );
                if( $xmxx=Bp::where('regid',$id)->where('xmid',$v[8])->where('is_print',0)->find() ){
                    Bp::where('regid',$id)
                        ->where('xmid',$v[8])
                        ->where('is_print',0)
                        ->update(['sl'=>$v[5]+$xmxx->sl,'je'=>$v[6]+$xmxx->je,'opt_user'=>$user->uname,'opt_datetime' => time(),'opt_name' => $user->name]);
                }else{
                    $insert[$k]['create_time'] = time();
                    Bp::insert($insert[$k]);
                }
            }
            $noprintxm = Bp::where('regid',$id)->where('is_print',0)->select();
            $tmp_noprintxm = array();
            foreach($noprintxm as $k=>$v){
                $tmp_noprintxm['data'][] = [$v['xmmc'],$v['sj'],$v['sl'],$v['je']];
            }
            if($btn_type=='print'){
                Bp::where('regid',$id)->where('is_print',0)->update(['is_print'=>1]);
            }
            $res = ['statusCode'=>200,'message'=>'操作完成','datas'=>json_encode($tmp_noprintxm)];
            //[[1,"00090","现场抬尸","次",0,1,0,"XCTS",90],[2,"00236","祭奠","次",10,1,10,"JD",236],[3,"00306","尸袋","条",200,1,200,"SD",306],[4,"00353","消毒费","项",10,1,10,"XDF",353],[5,"00361","血样试管","个",5,"2",10,"XYSG",361],[6,"00375","注射器","支",30,1,30,"ZSQ",375]]
        }else{
            $res = ['statusCode'=>300,'message'=>'数据录入错误'];
        }
        echo json_encode($res);
    }

    // 打印已录项目
    public function printbp(){
        $id = $this->request->param('id');
        $info = Register::get($id);
        $cbinfo = Db::name('register_cb')->where('reg_id',$id)->find();
        $csinfo = Db::name('register_cs')->where('reg_id',$id)->find();
        $gbtinfo = Db::name('register_gbt')->where('reg_id',$id)->find();
        $jsinfo = Db::name('register_js')->where('reg_id',$id)->find();
        $this->assign('info',$info);
        $this->assign('cbinfo',$cbinfo);
        $this->assign('csinfo',$csinfo);
        $this->assign('gbtinfo',$gbtinfo);
        $this->assign('jsinfo',$jsinfo);
        $this->assign('qzrq',date('Y年m月d日'));
        $bp = Bp::where('regid',$id)->select();
        $user = \app\admin\model\User::get(['uname'=>$this->request->session('efing.uname')]);
        $bpjsonObj = [];
        $i = 0;

        // 火化费
        if($cbinfo){
            if($cbinfo['ydlc']>0){
                $bpjsonObj[$i] = array(
                    $i+1,date('Y-m-d H:i:s',$cbinfo['opt_datetime']),'z01',
                    '火化费',$cbinfo['ydlfy'],1,$cbinfo['ydlfy'],$cbinfo['opt_user'],$cbinfo['opt_name']
                );
                $i++;
            }
        }

        // 告别厅费用
        if($gbtinfo){
            if($gbtinfo['gbtfy']>0){
                $bpjsonObj[$i] = array(
                    $i+1,date('Y-m-d H:i:s',$gbtinfo['opt_datetime']),'z02',
                    $gbtinfo['gbt'], $gbtinfo['gbtfy'],1,$gbtinfo['gbtfy'],$gbtinfo['opt_user'],$gbtinfo['opt_name']
                );
                $i++;
            }
        }

        // 出殡费
        if($cbinfo){
            if($cbinfo['cbrq']>0){
                $bpjsonObj[$i] = array(
                    $i+1,date('Y-m-d H:i:s',$cbinfo['opt_datetime']),'z03',
                    $cbinfo['cbfs'],$cbinfo['cbfy'],1,$cbinfo['cbfy'],$cbinfo['opt_user'],$cbinfo['opt_name']
                );
                $i++;
            }
        }
        // 接尸费
        if($jsinfo){
            if($jsinfo['jsfy']>0){
                $bpjsonObj[$i] = array(
                    $i+1,date('Y-m-d H:i:s',$jsinfo['opt_datetime']),'z04',
                    '接尸',$jsinfo['jsfy'],1,$jsinfo['jsfy'],$jsinfo['opt_user'],$jsinfo['opt_name']
                );
                $i++;
            }
        }
        // 存尸费
        if($csinfo){
            $cssl = 0; // 存尸天数
            $getCsrq = date('Y-m-d H:i:s',$csinfo['cs_datetime']);
            $get12Time = strtotime(date('Y-m-d',time()).' 24:00:00');
            if($cbinfo and time()<$cbinfo['cbrq']){
                $getCbrqTime = date('Y-m-d H:i:s',strtotime(date('Y-m-d',$cbinfo['cbrq']).' 24:00:00'));
                $getCsrqTime = strtotime(date('Y-m-d',$csinfo['cs_datetime']).' 24:00:00');
                /*if($csinfo['cs_datetime'] < $getCsrqTime){
                    $csDay = 1;
                }else{
                    $csDay = 0;
                }*/
                $cssl = round($this->diffBetweenTwoDays($getCbrqTime,$getCsrq));
            }else{
                $getCurrentTime = date('Y-m-d H:i:s',time()); // 获取订单提交时24小时制的日期时间的
                // 判断存尸日期时间是否超过午夜12点
                if(time() < $get12Time){
                    $csDay = 1;
                }else{
                    $csDay = 0;
                }
                $cssl = round($this->diffBetweenTwoDays($getCurrentTime,$getCsrq))+$csDay;
            }
            $bpjsonObj[$i] = array(
                $i+1,date('Y-m-d H:i:s',$csinfo['opt_datetime']),'z05',
                '存尸费',$csinfo['cs_price'],$cssl,($csinfo['cs_price']*$cssl),$csinfo['opt_user'],$csinfo['opt_name']
            );
            $i++;
        }
        if($bp){
            foreach($bp as $k=>$v){
                $bpjsonObj[$k+$i] = array(
                    ($k+$i+1),date('Y-m-d H:i:s',$v['opt_datetime']),$v['xmbh'],
                    $v['xmmc'],$v['sj'],$v['sl'],
                    $v['je'],$v['opt_user'],$v['opt_name']
                );
            }
        }

        $printbp = array();
        foreach($bpjsonObj as $k=>$v){
            $printbp[$k] = array(
                $v[3],$v[4],$v[5],$v[6]
            );
        }
        Bp::where('regid',$id)->where('is_print',0)->update(['is_print'=>1]);
        $this->assign('bplist',json_encode($bpjsonObj));
        $this->assign('printbplist',json_encode($printbp));
        $this->assign('userinfo',$user);
        return $this->fetch('rcyw/qtgl/printbp');
    }

    // 打印订单
    public function showdj(){
        // 获取订单id
        $id = $this->request->param('id');
        $this->assign('info',Register::get($id));
        $this->assign('cbinfo',Db::name('register_cb')->where('reg_id',$id)->find());
        $this->assign('csinfo',Db::name('register_cs')->where('reg_id',$id)->find());
        $this->assign('gbtinfo',Db::name('register_gbt')->where('reg_id',$id)->find());
        $this->assign('jsinfo',Db::name('register_js')->where('reg_id',$id)->find());
        $this->assign('qzrq',date('Y年m月d日'));
        $this->assign('show',$this->request->param('show'));
        return $this->fetch('rcyw/qtgl/printdj');
    }


    /**
     * 求两个日期之间相差的天数
     * (针对1970年1月1日之后，求之前可以采用泰勒公式)
     * @param string $day1
     * @param string $day2
     * @return number
     */
    function diffBetweenTwoDays ($day1, $day2)
    {
        $second1 = strtotime($day1);
        $second2 = strtotime($day2);

        if ($second1 < $second2) {
            $tmp = $second2;
            $second2 = $second1;
            $second1 = $tmp;
        }
        return ($second1 - $second2) / 86400;
    }


}